<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\Table
{

    public function __construct(...$params) {
        parent::__construct(...$params);
        // 支持附表存储
        $this->is_data = 0;
        // 模板前缀(避免混淆)
        $this->tpl_prefix = 'content_';
        // 表单显示名称
        $this->name = dr_lang('自定义收款');
        $field = [
            'price' => array(
                'ismain' => 1,
                'name' => dr_lang('收款金额'),
                'fieldname' => 'price',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => 200,
                        'value' => ''
                    ),
                    'validate' => array(
                        'required' => 1,
                        'tips' => dr_lang('不填写或0表示捐赠方式，由用户自愿输入'),
                    )
                )
            ),
            'template' => array(
                'ismain' => 1,
                'name' => dr_lang('前台模板'),
                'fieldname' => 'template',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => 200,
                        'value' => 'show.html'
                    ),
                    'validate' => array(
                        'required' => 1,
                        'tips' => dr_lang('模板文件位于./gathering/**'),
                    )
                )
            ),
            'title' => array(
                'ismain' => 1,
                'name' => dr_lang('收款主题'),
                'fieldname' => 'title',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => 400,
                    ),
                    'validate' => array(
                        'required' => 1,
                    )
                )
            ),
            'url' => array(
                'ismain' => 1,
                'name' => dr_lang('相关地址'),
                'fieldname' => 'url',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => 400,
                    ),
                    'validate' => array(
                        'required' => 1,
                    )
                )
            ),
            'meta_title' => array(
                'ismain' => 1,
                'name' => dr_lang('SEO标题'),
                'fieldname' => 'meta_title',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => '95%',
                    ),
                    'validate' => array(
                        'required' => 1,
                    )
                )
            ),
            'meta_keywords' => array(
                'ismain' => 1,
                'name' => dr_lang('SEO关键字'),
                'fieldname' => 'meta_keywords',
                'fieldtype' => 'Text',
                'setting' => array(
                    'option' => array(
                        'width' => '95%',
                    )
                )
            ),
            'meta_description' => array(
                'ismain' => 1,
                'name' => dr_lang('SEO描述信息'),
                'fieldname' => 'meta_description',
                'fieldtype' => 'Textarea',
                'setting' => array(
                    'option' => array(
                        'width' => '95%',
                    )
                )
            ),
        ];
        $myfield = \Phpcmf\Service::M('field')->get_mytable_field('app_gathering', 0);
        $myfield && $field = dr_array22array($field, $myfield);
        // 初始化数据表
        $this->_init([
            'table' => 'app_gathering',
            'field' => $field,
            'date_field' => 'inputtime',
            'show_field' => 'title',
            'order_by' => 'inputtime desc',
        ]);
        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '自定义收款' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-cc-paypal'],
                    '添加' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/add', 'fa fa-plus'],
                    '修改' => ['hide:'.APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/edit', 'fa fa-edit'],
                    '查看' => ['hide:'.APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/show', 'fa fa-search'],
                    '自定义字段' => ['url:'.\Phpcmf\Service::L('Router')->url('field/index', ['rname'=>'table-app_gathering', 'rid'=>0]), 'fa fa-code'],
                ]
            ),
            'field' => $field
        ]);
    }

    // 后台查看列表
    public function index() {
        list($tpl) = $this->_List();
        \Phpcmf\Service::V()->display($tpl);
    }

    // 后台添加内容
    public function add() {
        list($tpl) = $this->_Post(0);
        \Phpcmf\Service::V()->display($tpl);
    }

    // 后台修改内容
    public function edit() {
        list($tpl) = $this->_Post(intval(\Phpcmf\Service::L('Input')->get('id')));
        \Phpcmf\Service::V()->display($tpl);
    }

    // 删除
    public function del() {
        $this->_Del(\Phpcmf\Service::L('Input')->get_post_ids());
    }

    // 后台查看内容
    public function show() {

        list($tpl, $data) = $this->_Show(intval(\Phpcmf\Service::L('Input')->get('id')));

        $this->_init([
            'table' => 'member_paylog',
            'order_by' => 'inputtime desc',
            'where_list' => '`mid` = "gathering-'.$data['id'].'-'.$this->init['table'].'"',
            'date_field' => 'inputtime',
        ]);

        $this->_List();

        \Phpcmf\Service::V()->assign([
            'data' => $data,
        ]);
        \Phpcmf\Service::V()->display($tpl);
    }

    // 格式化保存数据
    protected function _Format_Data($id, $data, $old) {

        if (!$id) {
            $data[1]['total'] = 0;
            $data[1]['inputtime'] = SYS_TIME;
            $data[1]['updatetime'] = 0;
            $data[1]['nums'] = 0;
        }

        return $data;
    }
}
